package tests;

import static util.Constant.STR_INVALID_ID;
import static util.Constant.STR_ITEM_LIST_ALREADY_EXIST;
import static util.Constant.STR_ITEM_LIST_NOT_EXIST;
import static util.Constant.STR_USER_ALREADY_FRIEND;
import static util.Constant.STR_USER_NOT_FRIEND;
import static util.Tool.Sha1;

import java.util.HashSet;
import junit.framework.Assert;
import org.junit.Test;

import base.User;

public class UserTest {

	@Test
	public void testAddFriend() {
		User user = new User(1, "toto", "toto", "toto", "toto",
				new HashSet<Integer>(), new HashSet<Integer>());
		try {
			user.addFriend(-1);
		} catch (Exception e) {
			Assert.assertEquals(STR_INVALID_ID, e.getMessage());
		}

		try {
			Integer friendID = new Integer(5);
			user.addFriend(friendID);
			user.addFriend(friendID);
		} catch (Exception e) {
			Assert.assertEquals(STR_USER_ALREADY_FRIEND, e.getMessage());
		}
	}

	@Test
	public void testAddItemList() {
		User user = new User(1, "toto", "toto", "toto", "toto",
				new HashSet<Integer>(), new HashSet<Integer>());
		try {
			user.addItemList(-1);
		} catch (Exception e) {
			Assert.assertEquals(STR_INVALID_ID, e.getMessage());
		}

		try {
			Integer itemListID = new Integer(5);
			user.addItemList(itemListID);
			user.addItemList(itemListID);
		} catch (Exception e) {
			Assert.assertEquals(STR_ITEM_LIST_ALREADY_EXIST, e.getMessage());
		}
	}

	@Test
	public void testConnect() {
		String password = "123kce";
		User user = new User(1, "toto", Sha1(password), "toto", "toto",
				new HashSet<Integer>(), new HashSet<Integer>());
		Assert.assertFalse(user.connect(password + "s"));
		Assert.assertTrue(user.connect(password));
	}

	@Test
	public void testRemoveFriend() {
		User user = new User(1, "toto", "toto", "toto", "toto",
				new HashSet<Integer>(), new HashSet<Integer>());
		try {
			user.removeFriend(-1);
		} catch (Exception e) {
			Assert.assertEquals(STR_INVALID_ID, e.getMessage());
		}

		try {
			user.removeFriend(5);
		} catch (Exception e) {
			Assert.assertEquals(STR_USER_NOT_FRIEND, e.getMessage());
		}
	}

	@Test
	public void testRemoveItemList() {
		User user = new User(1, "toto", "toto", "toto", "toto",
				new HashSet<Integer>(), new HashSet<Integer>());
		try {
			user.removeItemList(-1);
		} catch (Exception e) {
			Assert.assertEquals(STR_INVALID_ID, e.getMessage());
		}

		try {
			user.removeItemList(5);
		} catch (Exception e) {
			Assert.assertEquals(STR_ITEM_LIST_NOT_EXIST, e.getMessage());
		}
	}

	@Test
	public void testIsFriend() {
		User user = new User(4, "toto", "toto", "toto", "toto",
				new HashSet<Integer>(), new HashSet<Integer>());

		Integer friendID = new Integer(9);
		Assert.assertFalse(user.isFriend(friendID));

		try {
			user.addFriend(friendID);
			Assert.assertTrue(user.isFriend(friendID));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Test
	public void testUser() {
		try {
			@SuppressWarnings("unused")
			User user = new User(-1, "toto", "toto", "toto", "toto",
					new HashSet<Integer>(), new HashSet<Integer>());
		} catch (Exception e) {
			Assert.assertEquals(STR_INVALID_ID, e.getMessage());
		}

		try {
			@SuppressWarnings("unused")
			User user = new User(-1);
		} catch (Exception e) {
			Assert.assertEquals(STR_INVALID_ID, e.getMessage());
		}
	}
}
